package com.seya.calculite.app;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Arumugam on 9/28/2014.
 */
public class DatabaseHandler extends SQLiteOpenHelper {
    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;
    // Database Name
    private static final String DATABASE_NAME = "calculite";
    // Detailss table name
    private static final String TABLE_DETAILS = "details";
    // Detailss Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_VEHICLE = "vehicle";
    private static final String KEY_COMPANY = "company";
    private static final String KEY_START = "startlocation";
    private static final String KEY_END = "endlocation";
    private static final String KEY_TIME = "time";
    private static final String KEY_KM = "totalKM";
    private static final String KEY_TAG = "tag";
    private static final String KEY_DATE = "datecreated";


    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TABLE = "CREATE TABLE " + TABLE_DETAILS + "("
                /*+ KEY_ID + " INTEGER PRIMARY KEY,"*/ + KEY_VEHICLE + " TEXT," + KEY_COMPANY + " TEXT,"
                + KEY_START + " TEXT,"+ KEY_END + " TEXT,"+ KEY_TIME + " TEXT,"+ KEY_KM + " TEXT,"+ KEY_DATE + " TEXT,"
                + KEY_TAG + " TEXT" + ")";
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_DETAILS);
        // Create tables again
        onCreate(db);
    }

    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */
    // Adding new d
    void addDetails(Details d) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_VEHICLE, d.getVehicle());
        values.put(KEY_COMPANY, d.getCompany());
        values.put(KEY_START, d.getStartlocation());
        values.put(KEY_END, d.getEndlocation());
        values.put(KEY_TIME, d.getTime());
        values.put(KEY_KM, d.getKm());
        values.put(KEY_TAG, d.getTag());
        values.put(KEY_DATE, d.getDatecreated());
        // Inserting Row
        db.insert(TABLE_DETAILS, null, values);
        db.close(); // Closing database connection
    }
    // Getting single d
    Details getDetails(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_DETAILS, new String[] { KEY_COMPANY,
                        KEY_VEHICLE, KEY_START, KEY_END, KEY_TIME, KEY_KM, KEY_TAG, KEY_DATE }, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
        Details d = new Details(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4)
                , cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8));
        // return d
        return d;
    }

    // Getting All Detailss
    public List<Details> getAllDetailss() {
        List<Details> dList = new ArrayList<Details>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_DETAILS;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Details d = new Details();
                /*d.setId(Integer.parseInt(cursor.getString(0)));*/
                d.setVehicle(cursor.getString(0));
                d.setCompany(cursor.getString(1));
                d.setStartlocation(cursor.getString(2));
                d.setEndlocation(cursor.getString(3));
                d.setTime(cursor.getString(4));
                d.setKm(cursor.getString(5));
                d.setDatecreated(cursor.getString(6));
                d.setTag(cursor.getString(7));
                // Adding d to list
                dList.add(d);
            } while (cursor.moveToNext());
        }
        // return d list
        return dList;
    }

    public Details getLastDetail() {
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_DETAILS;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        // looping through all rows and adding to list
        cursor.moveToLast();

                Details d = new Details();
                /*d.setId(Integer.parseInt(cursor.getString(0)));*/
                d.setVehicle(cursor.getString(0));
                d.setCompany(cursor.getString(1));
                d.setStartlocation(cursor.getString(2));
                d.setEndlocation(cursor.getString(3));
                d.setTime(cursor.getString(4));
                d.setKm(cursor.getString(5));
                d.setDatecreated(cursor.getString(6));
                d.setTag(cursor.getString(7));
                // Adding d to list

        // return d list
        return d;
    }
    // Updating single d
    /*public int updateDetails(Details d) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, d.getName());
        values.put(KEY_PH_NO, d.getPhoneNumber());
        // updating row
        return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
                new String[] { String.valueOf(d.getID()) });
    }*/
    // Deleting single d
    /*public void deleteDetails(Details d) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
                new String[] { String.valueOf(d.getID()) });
        db.close();
    }*/
    // Getting ds Count
    public int getDetailssCount() {
        int count=0;
        String countQuery = "SELECT  * FROM " + TABLE_DETAILS;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        if(cursor != null && !cursor.isClosed()){
            count = cursor.getCount();
            cursor.close();
        }
        return count;
    }

    public void deleteAllDetails() {
        SQLiteDatabase db = this.getWritableDatabase();
        //db.delete(TABLE_DETAILS, KEY_ID + " = ?",
                //new String[] { String.valueOf(d.getID()) });
        db.delete(TABLE_DETAILS,null,null);
        db.close();
    }
}
